
package com.ms.orm.conditions.segments;


import com.ms.orm.conditions.ISqlSegment;
import com.ms.orm.enums.SqlKeyword;

import java.util.List;

import static java.util.stream.Collectors.joining;

/**
 * Group By SQL 片段
 *
 * @author miemie
 * @since 2018-06-27
 */
@SuppressWarnings("serial")
public class GroupBySegmentList extends AbstractISegmentList {

    @Override
    protected boolean transformList(List<ISqlSegment> list, ISqlSegment firstSegment, ISqlSegment lastSegment) {
       //直接删除传入的分组 sql 片段的第一个元素？ why?
        list.remove(0);
        return true;
    }

    @Override
    protected String childrenSqlSegment() {
        if (isEmpty()) {
            //原始sql片段 是空值，直接返回空字符串 ""
            return EMPTY;
        }
        //分组sql片段: having 字段1>=x ,字段2=y
        return this.stream().map(ISqlSegment::getSqlSegment).collect(joining(COMMA, SPACE + SqlKeyword.GROUP_BY.getSqlSegment() + SPACE, EMPTY));
    }
}
